<template>
  <div class="characterGraph-container">
    <el-card>
      <div id="characterGraph" :style="{width: width, height: height}"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getGraphData } from '@/api/echartsApi.js'
import { getColor } from '@/utils/Color.js'
import LesMiserables from '@/mock/jsonModule/LesMiserables.json'

export default {
  name: 'characterGraph',
  props:{
    height:{
      type: String,
      default: '200px'
    },
    width:{
      type: String,
      default: '200px'
    }
  },
  data(){
    return {
      LesMiserables: {}
    }
  },
  mounted () {
    this.$nextTick(() =>{
      this.initCharts()
    })
  },
  methods: {
    async getGraphData(){
      // 注释mock请求sss
      // const res = await getGraphData()
      // this.LesMiserables = res.data
      this.LesMiserables = LesMiserables
    },
    async initCharts(){
      const chartHeight = {
        height: this.height
      }
      const charts = echarts.init(document.getElementById('characterGraph'),null,chartHeight)
      await this.getGraphData()
      this.LesMiserables.nodes.forEach(function (node,index) {
        node.label = {
          show: node.symbolSize > 25
        };
        node.category = index
      });
      const Color = getColor()
      const option = {
        title: {
          text: 'Les Miserables',
          subtext: 'Circular layout',
          top: 'top',
          left: 'left'
        },
        color: Color,
        tooltip: {},
        legend: {
          type: 'scroll',
          orient: 'vertical',
          right: 10,
          top: 20,
          bottom: 20,
          data: this.LesMiserables.categories.map(function (a) {
            return a.name;
          })
        },
        animationDurationUpdate: 1500,
        animationEasingUpdate: 'quinticInOut',
        series: [
          {
            name: 'Les Miserables',
            type: 'graph',
            layout: 'circular',
            circular: {
              rotateLabel: true
            },
            data: this.LesMiserables.nodes,
            links: this.LesMiserables.links,
            categories: this.LesMiserables.categories,
            roam: true,
            label: {
              position: 'left',
              formatter: '{b}'
            },
            lineStyle: {
              color: 'source',
              curveness: 0.3   // 边的曲度，支持从 0 到 1 的值，值越大曲度越大
            }
          }
        ]
      };
      charts.setOption(option);
    },
  }
}
</script>

<style scoped>

</style>
